﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.ActivityFeed.Web.Tests.Samples
{
    public partial class SampleSubscribeUnsubscribe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Subscribe(object sender, EventArgs e)
        {
            var topics = GetTopics(sender);
            
            IActivityFeedService servie = ServiceLocator.Resolve<IActivityFeedService>();
            servie.Subscribe("user1", topics);
            
            this.RefreshSubscriptions(servie);
        }

        protected void Unsubscribe(object sender, EventArgs e)
        {
            var topics = GetTopics(sender);

            IActivityFeedService servie = ServiceLocator.Resolve<IActivityFeedService>();
            servie.Unsubscribe("user1", topics);
            
            this.RefreshSubscriptions(servie);
        }

        private void RefreshSubscriptions(IActivityFeedService servie)
        {
            this.rptSubscriptions.DataSource = servie.GetSubscriber("user1").Subscriptions;
            this.rptSubscriptions.DataBind();
        }

        private IEnumerable<Topic> GetTopics(object sender)
        {
            var topics = ((LinkButton)sender).CommandArgument as string;
            return topics.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t=>new Topic(t));
        }
    }
}